package com.ksyun.onepass.service.impl;

import com.ksyun.onepass.domain.po.User;
import com.ksyun.onepass.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    private final RedisTemplate<String,String> redisTemplate;

    @Override
    public List<User> queryUserAmounts(List<Long> uids) {
        List<User> users = new ArrayList<>();
        ExecutorService executor = Executors.newFixedThreadPool(50); // 创建一个线程池，您可以根据需求调整线程数
        List<Future<User>> futures = new ArrayList<>();

        // 根据ID数组从Redis中查询
        for (Long uid : uids) {
            Future<User> future = executor.submit(new Callable<User>() {
                @Override
                public User call() {
                    String amount = redisTemplate.opsForValue().get(uid.toString());
                    if (amount != null) {
                        return new User(uid, new BigDecimal(amount));
                    }
                    return null; // 返回null表示没有找到对应用户
                }
            });
            futures.add(future);
        }

        // 收集结果
        for (Future<User> future : futures) {
            try {
                User user = future.get();
                if (user != null) {
                    users.add(user);
                }
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace(); // 处理异常
            }
        }

        executor.shutdown(); // 关闭线程池
        return users.isEmpty() ? Collections.emptyList() : users;
    }

    @Override
    public BigDecimal getUserBalance(Long uid) {
        String amount = redisTemplate.opsForValue().get(uid.toString());
        if (amount != null) {
            return new BigDecimal(amount);
        }
        return new BigDecimal(0);
    }

    @Override
    public void deductAmount(Long sourceUid, BigDecimal amount) {
        redisTemplate.opsForValue().set(sourceUid.toString(),amount.toString());
    }

    @Override
    public void addAmount(Long targetUid, BigDecimal amount) {
        String s = redisTemplate.opsForValue().get(targetUid.toString());
        BigDecimal add=new BigDecimal(s).add(amount);
        redisTemplate.opsForValue().set(targetUid.toString(),add.toString());
    }
}
